# -*- coding: utf-8 -*-
import csv
import os

'''
https://github.com/qwd/LocationList
用 和风天气的国内城市列表，产生 WEB 和 固件使用的 城市查询数据
by .NFC 2023/07/14
'''
def containsLetter(string):
    return any(letter in string for letter in 'ABCDEF')

# 读取 CSV 文件
def read_csv_file(file_path):
    data = []
    with open(file_path, 'r', newline='', encoding='utf-8') as file:
        reader = csv.reader(file)
        for row in reader:
            #print(row)
            if row[0].find('China-City-List') != -1 :
                continue;
            if row[0].find('Location_ID') != -1 :
                continue;
            
            #if containsLetter(row[0]):  # 十六进制形式
            #    continue # nCityId = "0x" + row[0]
            if len(row[0]) != 9:
                continue
            else:                       # 十进制形式
                nCityId = hex(int(row[0]))
                
            newRow = [nCityId] + [row[0], row[1], row[2], row[7]]
                        
            data.append(newRow)
    
    sorted_list = sorted(data, key=lambda x: next(iter(x)))
    return sorted_list

def write_inc_file(file_path, data):
    '''for native'''
    with open(file_path, 'w', newline='', encoding='utf-8') as file:
        file.write("/** generated by city_parse.py */\n")
        file.write("#define CHN_CITY_COUNT %d\n" % (len(data)))
        file.write("const china_city_t china_city_map[CHN_CITY_COUNT] = {\n")
        
        for row in data:
            file.write("  { %s, /* \"%s\", */ \"%s\", \"%s\", \"%s\" },\n" % (row[0], row[1], row[2], row[3], row[4]))
        
        file.write("};\n")
        
def write_js_file(file_path, data):
    '''for web'''
    with open(file_path, 'w', newline='', encoding='utf-8') as file:
        file.write("/** generated by city_parse.py */\n")
        file.write("let cityCount = %d;\n" % (len(data)))
        file.write("let china_city_list = [\n")
        
        for row in data:
            file.write("  { /* id:%s, */ code:\"%s\", name_en:\"%s\", name_cn:\"%s\", prov:\"%s\" },\n" % (row[0], row[1], row[2], row[3], row[4]))
        
        file.write("]\n")

def write_py_file(file_path, data):
    '''for python'''
    with open(file_path, 'w', newline='', encoding='utf-8') as file:
        file.write("# generated by city_parse.py\n")
        file.write("# Count = %d\n" % (len(data)))
        file.write("china_citys = {\n")
        
        for row in data:
            file.write("  '%s': '%s',\n" % (row[1], row[3]))
        
        file.write("}\n")

str_flutter_class = '''
class CityEntity {
  CityEntity({
      required this.code,
      required this.cn,
      required this.en,
    });

  late String code;
  late String cn;
  late String en;
  
  @override
  String toString() {
    return '$cn-$en-$code';
  }
}
'''
def write_flutter_file(file_path, data):
    '''for flutter'''
    with open(file_path, 'w', newline='', encoding='utf-8') as file:
        file.write("// generated by city_parse.py\n")
        file.write("// Count = %d\n" % (len(data)))
        
        # class bed list
        # file.write(f"{str_flutter_class}\n\n")
        # file.write("int get cityTotal => listCityMap.length;\n\n")
        # file.write("List<CityEntity> listCityMap = [\n")
        
        # for row in data:
        #     file.write("  CityEntity(\n")
        #     file.write("    code: '%s',\n    cn: '%s',\n    en: '%s',\n" % (row[1], row[3], row[2]))
        #     file.write("  ),\n")
        # file.write("];\n\n")
        
        # for simply list
        file.write("abstract class CityMap {\n")
        file.write("  static const collocations = [\n")
        for row in data:
            code = row[1].replace("'", "`")
            cn = row[3].replace("'", "`")
            en = row[2].replace("'", "`")
            file.write("    '%s:%s:%s',\n" % (code, cn, en))  # 'code:cn:en'
        file.write("  ];\n")
        file.write("}\n")
        
        
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
print("当前工作目录:", os.getcwd())

cityMap = read_csv_file('China-City-List-latest.csv')
write_py_file("city_list.py", cityMap)  # for python firmware
write_inc_file("CityMap.inc", cityMap)  # for c/cpp firmware
write_js_file("citymap.js", cityMap)    # for webserver
write_flutter_file("city_map.dart", cityMap)    # for webserver

print("转换完成！")
